
*********CRSP/COMPUSTAT*********
use "$jfqa_rep/comp_crsp_ann_all.dta", clear

qui{
*define dates
rename datadate date
gen day=day(date)
gen month=month(date)
gen year=year(date)
gen monthyear=ym(year, month)

rename LPERMNO permno
sort permno monthyear

drop if permno==permno[_n+1] & year==year[_n+1]

sort permno year
tsset permno year

*calculate book equity
gen prefer=pstkrv
replace prefer=pstkl if missing(prefer)
replace prefer=pstk if missing(prefer)
replace prefer=0 if missing(prefer)

gen defer=txditc
replace txdb=0 if missing(txdb)
replace itcb=0 if missing(itcb)
replace defer=txdb+itcb if missing(defer)

gen stock=seq
replace stock=ceq+pstk if missing(stock)
replace stock=at-lt if missing(stock)

gen bookeqty=stock+defer-prefer
replace bookeqty=. if bookeqty<=0

gen profit=(revt-cogs)/at
gen asset=(at-L.at)/L.at

replace monthyear=monthyear+6

keep monthyear permno bookeqty profit asset

sort permno monthyear 
}

save "$jfqa_rep/11_Compustat_Controls.dta", replace
